/**
 * 
 */
package com.qachecklist.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qachecklist.domain.CheckList;

/**
 * @author hirokiterashima
 *
 */
public class CheckListController extends DomainController {

	private static final long serialVersionUID = 1L;
	
	protected String homePath = "checklist";
	
	protected String attributeName = "checklist";
	
	protected String listAttributeName = "checklists";
	
	protected static Class<CheckList> getObjectClass() {
		return CheckList.class;
	}
	
	protected String getHomePath() {
		return homePath;
	}
	
	@Override
	protected String getObjectAttributeName() {
		return attributeName;
	}

	@Override
	protected String getObjectListAttributeName() {
		return listAttributeName;
	}
	
	protected CheckList getObjectById(Long id) {
		return (CheckList) CheckList.getById(id, CheckList.class);
	}
	
	protected List<CheckList> getObjectList() {
		return (List<CheckList>) CheckList.getList(CheckList.class);
	}

	protected void doPost(
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action == null || action.equals("create")) {
			CheckList env = new CheckList();
			env.setDescription(request.getParameter("description"));
			env.setName(request.getParameter("name"));
			env.saveOrUpdate();
			response.sendRedirect(homePath);
		} else {
			 String id = request.getParameter("id");
			 CheckList env = (CheckList) CheckList.getById(new Long(id), CheckList.class);
			 env.setDescription(request.getParameter("description"));
			 env.setName(request.getParameter("name"));
			 env.saveOrUpdate();
			response.sendRedirect(homePath);
		}
    }
}